<?php
namespace app\controllers;

use pqphp\base\Controller;
use app\models\WebPayModel;
use app\pays\UnionPay;
use app\pays\WechatPay;
use app\models\QueryUserManager;
use app\models\RechargeManager;

class WebPayController extends Controller
{
    public function index()
    {
		header("Content-type: text/html; charset=utf-8");
		//(new RechargeManager())->Invok('E54DE42BA0CDF4FB23C3627BA0726BC6','PENGQI','13162371127','805363','23','1358.87','b18141c0-86c8-4a03-abd9-43005a33c4fe');
		//exit;
		//检测用户是否签约
		$id = isset($_GET['id']) ? $_GET['id'] : '';
		$pid = isset($_GET['pid']) ? $_GET['pid'] : '';
		$exetype=isset($_GET['exetype']) ? $_GET['exetype'] : '';
		
		//使用Session记录当前用户的唯一标识
		$_SESSION['pid']=$pid;
		$_SESSION['id']=$id;
		$_SESSION['exetype']=$exetype;
		//记录用户的IP地址
		$_SESSION['userip']=$this->getuserip();
		if(strlen($id)==0||strlen($pid)==0){
			$this->errormsg("非法请求");
		}
		//查找是否签约用户
		$model = new WebPayModel();
		$result = $model->finduser($id);
		if(count($result) == 0 || $result[0]['pid'] != $pid){
			$this->errormsg("非法用户");
		}
		$this->paylogs('用户成功进行身份认证');
		$paytype = $result[0]['paytype'];
		$_SESSION['paytype']=$paytype;
		$_SESSION['cardtype']=$result[0]['cardtype'];
		$_SESSION['rechargetype']=$result[0]['recharge'];//记录用户充值成功后进行的卡充值接口
		$this->assign('title', $result[0]['name']);
		$this->assign('logo', $result[0]['logopath']);
		//1:代表微信，2：代表支付宝。3：代表银联支付，4：代表其他支付
		switch ($paytype) {
			case 1:
				//代表微信支付
				$wechatpay=new WechatPay();
				$wechatpay->ExecuteMethod($model,0,'getopenid');
				$this->render();
				break;
			case 2:
				//代表支付宝支付
				$this->assign('title', '支付宝支付');
				$this->render();
				break;
			case 3:
				//代表银联支付
				$this->render();
				break;
			case 4:
				//代表扫码枪扫客户条码支付（微信刷卡支付）
				$this->render();
				break;
			default:;
				$this->errormsg("非法请求支付");
				break;
		}
    }

	public function getPayParam()
	{
		if(!isset($_SESSION['id'])){
			$this->errormsg("页面过期，请重新打开充值页面");
		}
		$id = $_SESSION['id'];
		$pid = $_SESSION['pid'];
		$exetype=$_SESSION['exetype'];
		//获取当前用户支付金额
		$money = isset($_POST['money']) ? floatval($_POST['money']) : 0.00;
		$phone = isset($_POST['phone']) ? $_POST['phone'] : '';
		$cardno = isset($_POST['cardno']) ? $_POST['cardno'] : '';
		$num = isset($_POST['num']) ? intval($_POST['num']) : 1;
		$money=$money*$num;
		if(strlen($phone)==0||strlen($cardno)==0){
			$this->errormsg("充值手机号码和卡号不能为空");
		}

		//判断用户是否真实
		$queryUser=new QueryUserManager();
		$_SESSION['oldmoney']=$queryUser->IsValidUser($_SESSION['rechargetype'],$phone,$cardno,$_SESSION['cardtype']);
		$_SESSION['phone']=$phone;
		$_SESSION['cardno']=$cardno;
		$_SESSION['money']=$money;

		//$this->errormsg("非法用户,禁止请求数据");
		//查找是否签约用户
		$model = new WebPayModel();
		$result = $model->finduser($id);
		if(count($result) == 0 || $result[0]['pid'] != $pid){
			$this->errormsg("非法用户,禁止请求数据");
		}
		$paytype = $result[0]['paytype'];
		//1:代表微信，2：代表支付宝。3：代表银联支付，4：代表其他支付
		switch ($paytype) {
			case 1:
				//代表微信支付
				$wechatpay=new WechatPay();
				$data = $wechatpay->ExecuteMethod($model,$money,$exetype);
				exit($data);
				break;
			case 2:
				//代表支付宝支付
				$this->errormsg("未开启支付宝支付");
				break;
			case 3:
				//代表银联支付
				$union = new UnionPay();
				$data = $union->ExecuteMethod($model,$money,$exetype);
				exit($data);
				break;
			default:
				$this->errormsg("非法请求支付");
				break;
		}
	}


	public function ScanCodePay()
	{
		//这个需要客户验签扫码支付，参数ascii升序+&key=MD5验证
		header("Content-type: text/html; charset=utf-8");
		//检测用户是否签约
		$id = isset($_GET['id']) ? $_GET['id'] : '';
		$pid = isset($_GET['pid']) ? $_GET['pid'] : '';
		$exetype=isset($_GET['exetype']) ? $_GET['exetype'] : '';
		$code = isset($_GET['code']) ? $_GET['code'] : '';
		$money = isset($_GET['money']) ? floatval($_GET['money']) : 0.00;

		//使用Session记录当前用户的唯一标识
		$_SESSION['pid']=$pid;
		$_SESSION['id']=$id;
		$_SESSION['exetype']=$exetype;
		//记录用户的IP地址
		$_SESSION['userip']=$this->getuserip();
		if(strlen($id)==0||strlen($pid)==0){
			$this->errormsg("非法请求");
		}
		//查找是否签约用户
		$model = new WebPayModel();
		$result = $model->finduser($id);
		if(count($result) == 0 || $result[0]['pid'] != $pid){
			$this->errormsg("非法用户");
		}
		$this->paylogs('用户成功进行身份认证');
		$paytype = $result[0]['paytype'];

		//1:代表微信，2：代表支付宝。3：代表银联支付，4：代表微信扫码支付
		switch ($paytype) {
			case 4:
				//代表扫码枪扫客户条码支付（微信刷卡支付）
				$wechatpay=new WechatPay();
				exit($wechatpay->ExecuteMethod($model,$money,$exetype,$code));
				break;
			default:;
				$this->errormsg("非法请求支付");
				break;
		}
	}

	public function SendTempMessage()
	{
		header("Content-type: text/html; charset=utf-8");
		$type=isset($_GET['type']) ? $_GET['type'] : '';
		//代表消费通知
		$wechatpay=new WechatPay();
		switch($type)
		{
			case 'audit':
				$title=isset($_GET['title']) ? $_GET['title'] : '';
				$number=isset($_GET['number']) ? $_GET['number'] : '';
				$company=isset($_GET['company']) ? $_GET['company'] : '';
				$desc=isset($_GET['desc']) ? $_GET['desc'] : '';
				$openid=isset($_GET['openid']) ? $_GET['openid'] : '';
				$result=$wechatpay->SendAuditMessage($title,$number,$company,$dec,$openid);
				break;
			case 'distr':
				$sid=isset($_GET['sid']) ? $_GET['sid'] : '';
				$title=isset($_GET['title']) ? $_GET['title'] : '';
				$number=isset($_GET['number']) ? $_GET['number'] : '';
				$len=isset($_GET['len']) ? $_GET['len'] : '';
				$desc=isset($_GET['desc']) ? $_GET['desc'] : '';
				$openid=isset($_GET['openid']) ? $_GET['openid'] : '';
				$result=$wechatpay->SendMatchMessage($title,$number,$len,$dec,$openid,$sid);
				break;
			case 'sent':
				$sid=isset($_GET['sid']) ? $_GET['sid'] : '';
				$title=isset($_GET['title']) ? $_GET['title'] : '';
				$number=isset($_GET['number']) ? $_GET['number'] : '';
				$openid=isset($_GET['openid']) ? $_GET['openid'] : '';
				$user=isset($_GET['user']) ? $_GET['user'] : '';
				$phone=isset($_GET['phone']) ? $_GET['phone'] : '';
				$address=isset($_GET['address']) ? $_GET['address'] : '';
				$totalmoney=isset($_GET['totalmoney']) ? $_GET['totalmoney'] : '';
				$remark=isset($_GET['remark']) ? $_GET['remark'] : '';
				$result=$wechatpay->SendSentMessage($sid,$number,$openid,$title,$user,$phone,$address,$totalmoney,$remark);
				break;
			default:
				$account=isset($_GET['account']) ? $_GET['account'] : '';
				$store=isset($_GET['store']) ? $_GET['store'] : '';
				$money=isset($_GET['money']) ? $_GET['money'] : '';
				$desc=isset($_GET['desc']) ? $_GET['desc'] : '';
				$result=$wechatpay->SendTemplateMessage($account,$store,$money,$desc);
				break;
		}
		exit(json_encode($result));
	}


	public function GetUserInfo()
	{
		header("Content-type: text/html; charset=utf-8");
		$usertype=isset($_GET['usertype']) ? $_GET['usertype'] : '';
		$url=isset($_GET['url']) ? $_GET['url'] : '';
		$guid=isset($_GET['guid']) ? $_GET['guid'] : '';
		$port=isset($_GET['port']) ? $_GET['port'] : '';
		$ip=isset($_GET['ip']) ? $_GET['ip'] : '';
		switch ($usertype) {
			case 'wxopenid':
				//代表扫码枪扫客户条码支付（微信刷卡支付）
				$wechatpay=new WechatPay();
				$data = $wechatpay->RequestUserInfo($usertype);
				break;
			case 'wxuserinfo':
				//代表扫码枪扫客户条码支付（微信刷卡支付）
				$wechatpay=new WechatPay();
				$data = $wechatpay->RequestUserInfo($usertype);
				break;
			default:;
				$this->errormsg("非法请求获取用户信息");
				break;
		}
		$param=json_encode($data);
		Header("Location:".$url.'?param='.$param."&guid=".$guid.'&port='.$port.'&ip='.$ip);
	}


	/*
	*微信支付通知接口
	*/
	public function WechatPayNotice()
	{
		$this->paylogs('微信支付成功回调开始');
		$wechatpay=new WechatPay();
		$wechatpay->WechatNotice();
	}

	/*
	*银联支付通知接口
	*/
	public function UnionPayNotice()
	{
		$this->paylogs('银联支付成功回调开始');
		$unionpay=new UnionPay();
		$unionpay->UnionNotice();
	}

}